/**
 * Class for control of products on catalog and search page
 */
function ProductsControl(){
    
    /**
     * Current object
     */
    var self = this;
    
    /**
     * Load filtered orders
     */
    this.getContent = function (ajaxUrl){
        
        jQuery.ajax({
            url: ajaxUrl,
            type: 'POST',
            success: function(data) {
                jQuery('.products').html(data);
                jQuery('.pagination a').click(self.doPagination);
                jQuery('#products').submit(self.doSubmit);
                jQuery('#rows').change(self.doSubmit);
                jQuery('#sort').change(self.doSubmit);
                jQuery('#direction').change(self.doSubmit);
            }
        });
        
    }
    
    this.doSubmit = function(){
        var formUrl = jQuery(location).attr('href').replace('#','')+'?'+jQuery('#products').serialize();
        self.getContent(formUrl);
    }
    
    this.doPagination = function(){
        var url = jQuery(this).attr('href');
        self.getContent(url);
        return false;
    }
}

/**
 * Initialize document
 */
jQuery(document).ready(
    function(){
        var productsControl = new ProductsControl();
        jQuery('#rows').change(productsControl.doSubmit);
        jQuery('#sort').change(productsControl.doSubmit);
        jQuery('#direction').change(productsControl.doSubmit);
        jQuery('.pagination a').click(productsControl.doPagination);
    }
    );
